import Mock from 'mockjs'

// OrderList
let orderList = [];
if (localStorage.getItem('order_list')) {
    orderList = JSON.parse(localStorage.getItem('order_list'));
} else {
    orderList = Mock.mock({
        'list|20': [
            {
                'id|+1': 1,
                name: '@cname',
                'phone|13000000000-19999999999': 1,
                'payment|10-100': 1
            }
        ]
    })
    localStorage.setItem('order_list', JSON.stringify(orderList));
}

Mock.mock('/api/order/list', 'get', () => {
    return orderList;
})

// 历史记录
let record = {};
if (localStorage.getItem('record')) {
    record = JSON.parse(localStorage.getItem('record'));
} else {
    record = Mock.mock({
        'list|40': [
            {
                'id|+1': 1,
                name: '@cname',
                'phone|13000000000-19999999999': 1,
                'payment|10-100': 1,
                date: new Date().toLocaleDateString(),
                'oredrgood1|1': ['米饭', '馒头', '玉米', '蒸饺', "煎饼", '包子'],
                'oredrgood2|1': ['橙汁', '可乐', '酸梅汤', '冰封', '雪花啤酒'],
                'oredrgood3|1': ["肉臊子面", '炸酱面', '鸡汤刀削面', '油泼面', "炒面", "大盘鸡拌面", "麻婆豆腐", "葱花鸡蛋", "水煮肉片", "青椒肉丝", "酸菜鱼", "宫保鸡丁"],

            }
        ]
    })
    localStorage.setItem('record', JSON.stringify(record));
}

Mock.mock('/api/record/list', 'get', () => {
    return record;
})
// 历史订单详情
Mock.mock('/api/record/details', 'post', (req) => {
    let { id } = JSON.parse(req.body);

    let item = record.list.find((item) => {
        return item.id == id;
    })
    if (item) {
        return item;
    }
})


// meal 推荐
let mealItem = [];
if (localStorage.getItem('meal_item')) {
    mealItem = JSON.parse(localStorage.getItem('meal_item'));
} else {
    mealItem = Mock.mock({
        'melist|4': [
            {
                'id|+1': 1,
                'meal|+1': ['推荐1', "推荐2", "推荐3", "推荐4"],
                'good1|+1': ['米饭', '肉臊子面', '大盘鸡拌面', '炸酱面'],
                'good2|+1': ['麻婆豆腐', '雪碧', '可乐', '皮蛋瘦肉粥'],
                'price|+1': [12, 15, 17, 20]
            }

        ]
    })
    localStorage.setItem('meal_item', JSON.stringify(mealItem));
}

Mock.mock('/api/meal/item', 'get', () => {
    return mealItem;
})
// 修改推荐
Mock.mock('/api/meal/sell', 'post', (req) => {
    let { id, good1, good2, price } = JSON.parse(req.body);
    mealItem.melist.forEach((item) => {
        if (item.id == id) {
            item.good1 = good1;
            item.good2 = good2;
            item.price = price;
        }
    })
    localStorage.setItem('meal_item', JSON.stringify(mealItem));
    return { code: 200, msg: '修改成功' }
})


// 获取goods
let goods = {};
if (localStorage.getItem('good_list')) {
    goods = JSON.parse(localStorage.getItem('good_list'));
} else {
    goods = Mock.mock({
        'list|14': [
            {
                'id|+1': 1,
                "good|+1": ['米饭', '馒头', "肉臊子面", '炸酱面', '鸡汤刀削面', '油泼面', "炒面", "大盘鸡拌面", "麻婆豆腐", "葱花鸡蛋", "水煮肉片", "青椒肉丝", "酸菜鱼", "宫保鸡丁"],
                'goodsPrices|+1': [2, 1, 11, 12, 12, 10, 13, 15, 15, 12, 22, 14, 48, 30],
            }
        ]
    })
    localStorage.setItem('good_list', JSON.stringify(goods));
}
Mock.mock('/api/goods/list', 'get', () => {
    return goods;
})

// 删除goods
Mock.mock('/api/goods/delete', 'post', (req) => {
    //接收参数 , req.body 是字符串格式,需要转为json对象
    // console.log( req.body );
    let { id } = JSON.parse(req.body);

    //通过id, 找到要删除的那一条数据, 执行删除
    let i = goods.list.findIndex((item) => {
        return item.id == id;
    })
    if (i != -1) {
        goods.list.splice(i, 1);
        localStorage.setItem('good_list', JSON.stringify(goods));
    }

    return { status: 200, msg: '删除成功!', list: goods }
})

// 修改goods
Mock.mock('/api/goods/edit', 'post', (req) => {
    let { id, good, goodsPrices } = JSON.parse(req.body);

    let i = goods.list.findIndex((item) => {
        return item.id == id;
    })
    if (i != -1) {
        goods.list.splice(i, 1, { id, good, goodsPrices });
        localStorage.setItem('good_list', JSON.stringify(goods));
    }
    return { status: 200, msg: '修改成功' }
})

// 添加good
Mock.mock('/api/goods/add', 'post', (req) => {
    let { good, goodsPrices } = JSON.parse(req.body);
    let idArr = [];
    goods.list.map((item) => {
        idArr.push(item.id);
    });

    let id = Math.round(Math.random() * 1000);
    let idrandom = () => {
        if (idArr.indexOf(id) == -1) {
            return id;
        } else {
            id = Math.round(Math.random() * 1000);
            idrandom();
        }

    };
    idrandom();
    goods.list.unshift({ id, good, goodsPrices });
    localStorage.setItem('good_list', JSON.stringify(goods));
})

// 注册
let register = [];
Mock.mock('/api/user/register', 'post', (req) => {
    let { phone, pass } = JSON.parse(req.body);
    if (localStorage.getItem('register')) {
        register = JSON.parse(localStorage.getItem('register'));
        let i = register.findIndex((item) => {
            return item.phone == phone;
        })
        if (i == -1) {
            register.push({ phone, pass });
            localStorage.setItem('register', JSON.stringify(register));
            return { code: 1, msg: '注册成功' }

        } else {
            return { code: 0, msg: '账号已存在' }
        }
    } else {
        register.push({ phone, pass });
        localStorage.setItem('register', JSON.stringify(register));
        return { code: 1, msg: '注册成功' }
    }

})
// 登录
Mock.mock('/api/user/login', 'post', (req) => {
    let { phone, pass } = JSON.parse(req.body);
    if (localStorage.getItem('register')) {
        let loginList = JSON.parse(localStorage.getItem('register'));
        for (let i = 0; i < loginList.length; i++) {
            if (loginList[i].phone / 1 == phone && loginList[i].pass == pass) {
                return { code: 1, msg: '登录成功' }
            }else if(loginList[i].phone / 1 == phone && loginList[i].pass != pass){
                return { code: 0, msg: '密码错误' }
            }
        }
        return { code: -1, msg: '账号不存在' }
    }else{
        return { code: -1, msg: '账号不存在' }
    }

})